package com.fr.report.cellElement;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.report.script.Calculator;
import com.fr.report.script.core.parser.UtilEvalError;
import com.fr.util.Utils;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class TableDataColumn
  implements Serializable, Cloneable
{
  protected transient Integer realColumnIndex;

  private TableDataColumn()
  {
    this.realColumnIndex = null;
  }

  public static TableDataColumn createColumn(String paramString)
  {
    return new ColumnName(paramString);
  }

  public static TableDataColumn createColumn(int paramInt)
  {
    return new ColumnIndex(paramInt);
  }

  public abstract int getColumnIndex(TableData paramTableData, Calculator paramCalculator);

  public static String getColumnName(TableDataColumn paramTableDataColumn)
  {
    if (paramTableDataColumn == null)
      return null;
    if (paramTableDataColumn instanceof ColumnName)
      return ((ColumnName)paramTableDataColumn).getName();
    return paramTableDataColumn.toString();
  }

  public static boolean isFirstColumn(TableDataColumn paramTableDataColumn, TableData paramTableData)
  {
    try
    {
      if ((paramTableData == null) || (paramTableData.getColumnCount() <= 0))
        return false;
    }
    catch (TableDataException localTableDataException1)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException1.getMessage(), localTableDataException1);
      return false;
    }
    if (paramTableDataColumn instanceof ColumnIndex)
      return (((ColumnIndex)paramTableDataColumn).getIndex() == 0);
    if (paramTableDataColumn instanceof ColumnName)
      try
      {
        return ComparatorUtils.tableDataColumnNameEquals(((ColumnName)paramTableDataColumn).getName(), paramTableData.getColumnName(0));
      }
      catch (TableDataException localTableDataException2)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException2.getMessage(), localTableDataException2);
        return false;
      }
    return false;
  }

  public static boolean isLastColumn(TableDataColumn paramTableDataColumn, TableData paramTableData)
  {
    if (paramTableData == null)
      return false;
    int i = 0;
    try
    {
      i = paramTableData.getColumnCount();
    }
    catch (TableDataException localTableDataException1)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException1.getMessage(), localTableDataException1);
    }
    if (i <= 0)
      return false;
    if (paramTableDataColumn instanceof ColumnIndex)
      return (((ColumnIndex)paramTableDataColumn).getIndex() == i - 1);
    if (paramTableDataColumn instanceof ColumnName)
      try
      {
        return ComparatorUtils.tableDataColumnNameEquals(((ColumnName)paramTableDataColumn).getName(), paramTableData.getColumnName(i - 1));
      }
      catch (TableDataException localTableDataException2)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException2.getMessage(), localTableDataException2);
        return false;
      }
    return false;
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }

  public static TableDataColumn readXML(XMLableReader paramXMLableReader)
  {
    String str;
    if ((str = paramXMLableReader.getAttr("columnName")) != null)
      return createColumn(str);
    if ((str = paramXMLableReader.getAttr("ci")) != null)
      return createColumn(Integer.parseInt(str));
    if ((str = paramXMLableReader.getAttr("columnIndex")) != null)
      return createColumn(Integer.parseInt(str) + 1);
    return null;
  }

  public static void writeXML(XMLPrintWriter paramXMLPrintWriter, TableDataColumn paramTableDataColumn)
  {
    if (paramTableDataColumn instanceof ColumnIndex)
      paramXMLPrintWriter.attr("ci", ColumnIndex.access$100((ColumnIndex)paramTableDataColumn));
    else if (paramTableDataColumn instanceof ColumnName)
      paramXMLPrintWriter.attr("columnName", ColumnName.access$200((ColumnName)paramTableDataColumn));
  }

  TableDataColumn(1 param1)
  {
  }

  private static class ColumnName extends TableDataColumn
  {
    private String name;

    public ColumnName(String paramString)
    {
      super(null);
      this.name = paramString;
    }

    public String getName()
    {
      return this.name;
    }

    public int getColumnIndex(TableData paramTableData, Calculator paramCalculator)
    {
      if (this.realColumnIndex == null)
      {
        if (this.name.startsWith("="));
        try
        {
          Object localObject = paramCalculator.eval(this.name.substring(1));
          if (localObject instanceof Number)
            return createColumn(((Number)localObject).intValue()).getColumnIndex(paramTableData, paramCalculator);
          return createColumn(Utils.objectToString(localObject)).getColumnIndex(paramTableData, paramCalculator);
        }
        catch (UtilEvalError i)
        {
          FRContext.getLogger().log(Level.WARNING, localUtilEvalError.getMessage(), localUtilEvalError);
          this.realColumnIndex = new Integer(-2147483648);
          if (this.name.matches("^#\\d+$"))
          {
            this.realColumnIndex = new Integer(createColumn(Integer.parseInt(this.name.substring(1))).getColumnIndex(paramTableData, paramCalculator));
            break label241:
          }
          int i = 0;
          try
          {
            i = paramTableData.getColumnCount();
          }
          catch (TableDataException localTableDataException1)
          {
            FRContext.getLogger().log(Level.WARNING, localTableDataException1.getMessage(), localTableDataException1);
          }
          int j = 0;
          if (j >= i)
            break label241;
          try
          {
            if (ComparatorUtils.tableDataColumnNameEquals(this.name, paramTableData.getColumnName(j)))
            {
              this.realColumnIndex = new Integer(j);
              break label241:
            }
          }
          catch (TableDataException localTableDataException2)
          {
            FRContext.getLogger().log(Level.WARNING, localTableDataException2.getMessage(), localTableDataException2);
          }
          ++j;
        }
      }
      label241: return this.realColumnIndex.intValue();
    }

    public boolean equals(Object paramObject)
    {
      return ((paramObject instanceof ColumnName) && (((ColumnName)paramObject).name.equals(this.name)));
    }

    public int hashCode()
    {
      return this.name.hashCode();
    }

    public String toString()
    {
      return this.name;
    }

    static String access$200(ColumnName paramColumnName)
    {
      return paramColumnName.name;
    }
  }

  private static class ColumnIndex extends TableDataColumn
  {
    private int index;

    public ColumnIndex(int paramInt)
    {
      super(null);
      this.index = paramInt;
    }

    public int getIndex()
    {
      return this.index;
    }

    public int getColumnIndex(TableData paramTableData, Calculator paramCalculator)
    {
      if (this.realColumnIndex == null)
      {
        this.realColumnIndex = new Integer(-2147483648);
        try
        {
          if ((this.index >= 0) && (this.index <= paramTableData.getColumnCount()))
            this.realColumnIndex = new Integer(this.index - 1);
        }
        catch (TableDataException localTableDataException)
        {
          FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
        }
      }
      return this.realColumnIndex.intValue();
    }

    public boolean equals(Object paramObject)
    {
      return ((paramObject instanceof ColumnIndex) && (((ColumnIndex)paramObject).index == this.index));
    }

    public int hashCode()
    {
      return this.index;
    }

    public String toString()
    {
      return "#" + this.index;
    }

    static int access$100(ColumnIndex paramColumnIndex)
    {
      return paramColumnIndex.index;
    }
  }
}